package com.example.zhidao.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.zhidao.model.po.Admin;
import com.example.zhidao.model.po.Shuju;
import com.example.zhidao.model.responseData.ResponseData;
import com.example.zhidao.service.AdminService;
import com.example.zhidao.service.ShujuService;
import com.example.zhidao.utils.Ujwt;
import io.jsonwebtoken.Claims;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

@RestController
@CrossOrigin
@RequestMapping("/shuju")
public class ShujuController {//1


    @Autowired
    private AdminService adminService;

    @Autowired
    private Ujwt ujwt;

    @Autowired
    private ShujuService shujuService;

    @GetMapping("/admin/list")
    public ResponseData list( @RequestHeader String token, @Param("date") String date,@Param("name") String name, Page page){
        Claims claims = (Claims) ujwt.tokenToOut(token);//强制转换属性
        String aid = claims.getId();
        QueryWrapper<Admin> q = new QueryWrapper();
        q.eq("id", aid);
        Admin a = adminService.getOne(q);
        if (a == null) {
            return new ResponseData(null, false, "你不是管理员", 300);
        }
        if (a.getAuth() != 0) {
            return new ResponseData(null, false, "你不是Boss", 400);
        }
        QueryWrapper<Shuju> qw=new QueryWrapper<>();
        qw.likeRight(date!="","DATE_FORMAT(createtime, '%Y-%m-%d')", date);
        qw.eq(name!="","username",name);

        Page<Shuju> shujuPage=shujuService.page(page,qw);

        if(shujuPage!=null){
            return new ResponseData(shujuPage,true,"获取成功",200);
        }else{
            return new ResponseData(null,false,"获取失败",201);
        }
    }
}
